from typing import Dict, List, Any, Optional

# 产品计划相关工具定义
class ProductPlanTools:
    def __init__(self, http_client):
        self.http = http_client
    
    def get_plans(self, params: Optional[Dict[str, Any]] = None):
        """获取产品计划列表"""
        print('获取产品计划列表')
        response = self.http.get('/api.php/v1/productplans', params=params)
        print(response)
        return response
        
    def create_plan(self, data: Dict[str, Any]):
        """创建产品计划"""
        print('创建产品计划')
        response = self.http.post('/api.php/v1/productplans', json=data)
        print(response)
        return response
        
    def get_plan_detail(self, plan_id: int):
        """获取计划详情"""
        print(f'获取计划ID={plan_id}的详情')
        response = self.http.get(f'/api.php/v1/productplans/{plan_id}')
        print(response)
        return response
        
    def update_plan(self, plan_id: int, data: Dict[str, Any]):
        """修改计划"""
        print(f'修改计划ID={plan_id}的信息')
        response = self.http.put(f'/api.php/v1/productplans/{plan_id}', json=data)
        print(response)
        return response
        
    def delete_plan(self, plan_id: int):
        """删除计划"""
        print(f'删除计划ID={plan_id}')
        response = self.http.delete(f'/api.php/v1/productplans/{plan_id}')
        print(response)
        return response
        
    def link_story(self, plan_id: int, data: Dict[str, Any]):
        """产品计划关联需求"""
        print(f'计划ID={plan_id}关联需求')
        response = self.http.post(f'/api.php/v1/productplans/{plan_id}/stories', json=data)
        print(response)
        return response
        
    def unlink_story(self, plan_id: int, story_id: int):
        """产品计划取消关联需求"""
        print(f'计划ID={plan_id}取消关联需求ID={story_id}')
        response = self.http.delete(f'/api.php/v1/productplans/{plan_id}/stories/{story_id}')
        print(response)
        return response
        
    def link_bug(self, plan_id: int, data: Dict[str, Any]):
        """产品计划关联Bug"""
        print(f'计划ID={plan_id}关联Bug')
        response = self.http.post(f'/api.php/v1/productplans/{plan_id}/bugs', json=data)
        print(response)
        return response
        
    def unlink_bug(self, plan_id: int, bug_id: int):
        """产品计划取消关联Bug"""
        print(f'计划ID={plan_id}取消关联Bug ID={bug_id}')
        response = self.http.delete(f'/api.php/v1/productplans/{plan_id}/bugs/{bug_id}')
        print(response)
        return response
    
    # 用于注册工具到MCP
    def register(self, mcp_instance):
        """
        将所有产品计划工具注册到MCP实例
        
        Args:
            mcp_instance: FastMCP实例
        """
        mcp_instance.add_tool(self.get_plans, name="get_plans", description="获取产品计划列表。参数：可选params(Dict)包含筛选条件如product(产品ID)、status(状态)、limit(每页数量)、page(页码)等。产品ID可通过get_products获取。不传参数时获取所有产品计划。")
        
        mcp_instance.add_tool(self.create_plan, name="create_plan", description="创建产品计划。参数：data(Dict，必填)包含计划信息，必填字段有product(产品ID，从get_products获取)、title(计划名称)、begin(计划开始日期，格式如'2023-01-01')、end(计划结束日期)，可选字段有desc(描述)、status(状态)等。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.get_plan_detail, name="get_plan_detail", description="获取计划详情。参数：plan_id(int，必填)计划ID。计划ID可从get_plans方法返回的列表中获取。")
        
        mcp_instance.add_tool(self.update_plan, name="update_plan", description="修改计划信息。参数：plan_id(int，必填)计划ID；data(Dict，必填)包含要更新的字段，如title(计划名称)、begin(开始日期)、end(结束日期)、status(状态)等。计划ID可从get_plans获取，现有计划信息可从get_plan_detail获取。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.delete_plan, name="delete_plan", description="删除计划。参数：plan_id(int，必填)要删除的计划ID。计划ID可从get_plans方法返回的列表中获取。注意：删除计划可能影响关联的需求和Bug，请谨慎操作。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.link_story, name="link_story", description="产品计划关联需求。参数：plan_id(int，必填)计划ID；data(Dict，必填)包含stories字段，值为需求ID数组，如{\"stories\": [101, 102]}。计划ID可从get_plans获取，需求ID可从产品需求列表获取。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.unlink_story, name="unlink_story", description="产品计划取消关联需求。参数：plan_id(int，必填)计划ID；story_id(int，必填)需求ID。计划ID可从get_plans获取，需求ID可从计划详情中获取。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.link_bug, name="link_bug", description="产品计划关联Bug。参数：plan_id(int，必填)计划ID；data(Dict，必填)包含bugs字段，值为Bug ID数组，如{\"bugs\": [201, 202]}。计划ID可从get_plans获取，Bug ID可从get_bugs获取。需要产品负责人或管理员权限。")
        
        mcp_instance.add_tool(self.unlink_bug, name="unlink_bug", description="产品计划取消关联Bug。参数：plan_id(int，必填)计划ID；bug_id(int，必填)Bug ID。计划ID可从get_plans获取，Bug ID可从计划详情中获取。需要产品负责人或管理员权限。") 